var is_valid_username   = false;
var is_valid_email      = false;
var is_valid_password   = false;

$(document).ready(function() {
    // Run these methods when the DOM is ready in case the page is refreshed while the fields are filled-in
    usernameExists();
    emailExists();
    loadCaptcha();

    // Bind callback functions to input field events
    $("#username").bind('change keyup', function() { usernameExists(); });
    $("#email").bind('change keyup', function() { emailExists(); });
    $("#password").bind('change keyup', function() { togglePasswordsDoesntMatch(); });
    $("#retype_password").bind('change keyup', function() { togglePasswordsDoesntMatch(); });
    $("#reload_captcha").bind('click', function() { loadCaptcha(); });

    // bind form using ajaxForm
    $("#signup_form").ajaxForm({
        // target identifies the element(s) to update with the server response
        target: "#signup_body",
        // pre-submit callback
        beforeSubmit: function() {
            $("#signup_body").html("Please wait...");
        }
    });

});

function loadCaptcha() {
    // Calls the 'captcha' function and loads the result in the 'captcha_image' div element
    $("#captcha_image").load('welcome/captcha');
}

function usernameExists() {
    // Uses an AJAX call to the 'username_exists' function of the 'user' controller
    $.post('user/username_exists', { username : $("#username").val() } ,
        function(data) {
            toggleUsernameExists(data);
        }
    );
}

function emailExists() {
    // Uses an AJAX call to the 'email_exists' function of the 'user' controller
    $.post('user/email_exists', { email : $("#email").val() } ,
        function(data) {
            toggleEmailExists(data);
        }
    );
}

function toggleUsernameExists(data) {
    $("#username_exists").html(data);

    if (data == "") {
        is_valid_username = true;
    }
    else {
        is_valid_username = false;
    }

    toggleSubmit();
}

function toggleEmailExists(data) {
    $("#email_exists").html(data);

    if (data == "") {
        is_valid_email = true;
    }
    else {
        is_valid_email = false;
    }

    toggleSubmit();
}

function togglePasswordsDoesntMatch() {
    if (jQuery.trim($("#password").val()) == "" &&
        jQuery.trim($("#retype_password").val()) == "") {
        $('#passwords_doesnt_match').html("Password fields are required");
        is_valid_password = false;
    } else {
        if (jQuery.trim($("#password").val()) ==
            jQuery.trim($("#retype_password").val())) {
            $('#passwords_doesnt_match').html("");
            is_valid_password = true;
        } else {
            $('#passwords_doesnt_match').html("Passwords doesn't match");
            is_valid_password = false;
        }
    }

    toggleSubmit();
}

function toggleSubmit() {
    if (is_valid_username && is_valid_email && is_valid_password) {
        $("#submit").removeAttr("disabled");
    } else {
        $("#submit").attr("disabled", true);
    }
    $("#submit").removeAttr("disabled");
}